﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.Models;
namespace web.Controllers
{
    public class MONController : Controller
    {
        QLBANHANGEntities db = new QLBANHANGEntities();
        //
        // GET: /MON/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult creat()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult creat(FormCollection conection, MON monhoc)
        {
            string smamon = conection["txtmamon"];
            string stenmon = conection["txttenmon"];
            int isotiet = int.Parse(conection["txtsotiet"]);
            int ithutu = int.Parse(conection["txtthutu"]);

            monhoc.mamon = smamon;
            monhoc.tenmon = stenmon;
            monhoc.sotiet = isotiet;
            monhoc.thutu = ithutu;
            db.MONs.Add(monhoc);
            db.SaveChanges();
            return creat();
        }
        public ActionResult edit(int id)
        {
            var monhoc = db.MONs.First(m =>m.ID == id);
            return View(monhoc);
        }
        [HttpPost]
        public ActionResult edit(int id, FormCollection connection)
        {
            var monhoc = db.MONs.First(m =>m.ID == id);
            string smamon = connection["txtmamon"];
            string stenmon = connection["txttenmon"];
            int isotiet = int.Parse(connection["txtsotiet"]);
            int ithutu = int.Parse(connection["txtthutu"]);
            string Images = connection["Images"];
            string t = Images;
            string Detail = connection["txtDetail"];
            string sDetail = Detail;
            monhoc.ID = id;
            monhoc.mamon = smamon;
            monhoc.tenmon = stenmon;
            monhoc.sotiet = isotiet;
            monhoc.thutu = ithutu;
            UpdateModel(monhoc);
            db.SaveChanges();
            return RedirectToAction("edit");
        }
    }
}
